#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2020/3/19  9:21
# @Author: 余浪人
# @email: yulangren520@gmail.com
from wtforms import Form, StringField, validators, ValidationError
from flask import request

class Install_Form(Form):
    mysql_db_host = StringField(validators=[validators.DataRequired('请填写MySQL主机地址')])
    mysql_db_name = StringField(validators=[validators.DataRequired('请填写MySQL数据库名称'),validators.Length(max=128,message='数据库名称过长')])
    mysql_db_port = StringField(validators=[validators.DataRequired('请填写MySQL主机端口号')])
    mysql_db_username = StringField(validators=[validators.DataRequired('请填写MySQL用户名'),validators.Length(max=128,message='数据库用户名过长')])
    mysql_db_password = StringField(validators=[validators.DataRequired('请填写MySQL密码'),validators.Length(max=128,message='数据库密码过长')])
    redis_db_host = StringField(validators=[validators.DataRequired('请填写Redis主机地址')])
    redis_db_port = StringField(validators=[validators.DataRequired('请填写Redis主机端口')])
    redis_db_password = StringField()
    username = StringField(validators=[validators.DataRequired('请填写用户名')])
    password = StringField(validators=[validators.DataRequired('请填写密码'),validators.Length(3,message='密码至少3位')])
    web_domain = StringField()

    def validate_username(self, _):
        if (request.cookies.get('status_1')!='true') or (request.cookies.get('status_2')!='true'):
            raise ValidationError("请先数据库连接测试！")

    def validate_redis_db_port(self,redis_db_port):
        try:
            int(redis_db_port.data)
        except:
            raise ValidationError("Redis配置端口错误！")

    def validate_mysql_db_port(self,mysql_db_port):
        try:
            int(mysql_db_port.data)
        except:
            raise ValidationError("MySQL配置端口错误！")